#include "picbutton.h"

PicButton::PicButton(QWidget *parent)
    :QPushButton(parent)
{

}

void PicButton::SetIcons(const QString &normal, const QString &hover, const QString &pressed)
{
    _normal = normal;
    _hover = hover;
    _pressed = pressed;

    QPixmap tmpPixmap;
    tmpPixmap.load(normal);
    this->resize(tmpPixmap.size());//让按钮的大小去适应图标的大小
    this->setIcon(tmpPixmap);
    this->setIconSize(tmpPixmap.size());//让图标大小和原图片大小保持一致避免失真
}

bool PicButton::event(QEvent *e)
{
    auto type = e->type();
    if(type==QEvent::Enter){//鼠标进入按钮范围
        setHoverIcon();
    }else if(type == QEvent::Leave){//鼠标离开按钮范围
        setNormalIcon();
    }else if(type == QEvent::MouseButtonPress){//鼠标点击
        setPressIcon();
    }else if(type == QEvent::MouseButtonRelease){//鼠标松开
        setHoverIcon();
    }
    return QPushButton::event(e);
}

void PicButton::setNormalIcon()
{
    QPixmap tmpPixmap;
    tmpPixmap.load(_normal);
    this->resize(tmpPixmap.size());
    this->setIcon(tmpPixmap);
    this->setIconSize(tmpPixmap.size());
}

void PicButton::setHoverIcon()
{
    QPixmap tmpPixmap;
    tmpPixmap.load(_hover);
    this->resize(tmpPixmap.size());
    this->setIcon(tmpPixmap);
    this->setIconSize(tmpPixmap.size());
}

void PicButton::setPressIcon()
{
    QPixmap tmpPixmap;
    tmpPixmap.load(_pressed);
    this->resize(tmpPixmap.size());
    this->setIcon(tmpPixmap);
    this->setIconSize(tmpPixmap.size());
}

